package com.connector.server.util;

import org.slf4j.LoggerFactory;
import org.apache.http.HttpResponse;
import java.util.Iterator;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import java.io.UnsupportedEncodingException;
import com.alibaba.fastjson.JSON;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.HttpEntity;
import java.util.List;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import com.google.common.collect.Lists;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClients;
import java.util.HashMap;
import com.connector.base.config.InstantConfig;
import java.util.Map;
import org.slf4j.Logger;

public class HttpUtil
{
    private static final Logger log;
    private static final int TIME_OUT = 10000;

    public static String postToInstantServer_getNoReadMsgByUser(final Map<String, String> paramMap) throws Exception {
        return post(InstantConfig.server.business.instantServerUrl + InstantConfig.server.business.instantGetAllMsgUrl, paramMap);
    }

    public static String postToInstantServer_updateMsgStatus(final Map<String, String> paramMap) throws Exception {
        return post(InstantConfig.server.business.instantServerUrl + InstantConfig.server.business.instantUpdateMsgStatusUrl, paramMap);
    }

    public static String postToInstantServer_addMessage(final Map<String, String> paramMap) throws Exception {
        return post(InstantConfig.server.business.instantServerUrl + InstantConfig.server.business.instantAddMessageUrl, paramMap);
    }

    public static String postToInstantServer_getGroupDetail(final Map<String, String> paramMap) throws Exception {
        return post(InstantConfig.server.business.instantServerUrl + InstantConfig.server.business.instantGetGroupDetailUrl, paramMap);
    }

    public static String transgerWithLogin() throws Exception {
        return post(InstantConfig.server.jeplantform.jeServerUrl + InstantConfig.server.jeplantform.transgerWithLogin, null);
    }

    public static String transgerWithLoginApp(final String appId, final String msg) throws Exception {
        final Map paramMap = new HashMap();
        paramMap.put("appId", appId);
        paramMap.put("msg", msg);
        return post(InstantConfig.server.jeaplantformapp.jeAppServerUrl + InstantConfig.server.jeaplantformapp.transgerAppWithLogin, paramMap);
    }

    public static String pushErrorCertStatus() throws Exception {
        return post(InstantConfig.server.jeplantform.jeServerUrl + InstantConfig.server.jeplantform.pushCertStatus, null);
    }

    private static String post(final String url, final Map<String, String> paramMap) throws Exception {
        CloseableHttpClient httpClient = null;
        final long start = System.currentTimeMillis();
        try {
            httpClient = HttpClients.createDefault();
            final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000).setRedirectsEnabled(true).build();
            final HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            if (null != paramMap && !paramMap.isEmpty()) {
                final List<BasicNameValuePair> list =  Lists.newArrayList();
                for (final Map.Entry<String, String> entry : paramMap.entrySet()) {
                    final Object value = entry.getValue();
                    final StringBuilder sb = new StringBuilder();
                    sb.append((String)value);
                    list.add(new BasicNameValuePair((String)entry.getKey(), sb.toString()));
                }
                final UrlEncodedFormEntity entity = new UrlEncodedFormEntity((List)list, "UTF-8");
                httpPost.setEntity((HttpEntity)entity);
            }
            final HttpResponse httpResponse = (HttpResponse)httpClient.execute((HttpUriRequest)httpPost);
            if (httpResponse == null) {
                HttpUtil.log.error("[" + url + "]请求失败,返回的数据为空!");
                throw new Exception("请求失败,返回的数据为空!");
            }
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                final String strResult = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
                HttpUtil.log.info("[" + url + "]请求时间为:" + (System.currentTimeMillis() - start) + "--参数" + JSON.toJSONString((Object)paramMap) + "+结果:" + strResult);
                return strResult;
            }
            HttpUtil.log.error("[" + url + "]请求失败," + httpResponse.getStatusLine().toString());
            throw new Exception("请求失败," + httpResponse.getStatusLine().toString());
        }
        catch (UnsupportedEncodingException e) {
            HttpUtil.log.error("[" + url + "]请求失败" + e.getMessage());
            throw e;
        }
        catch (ClientProtocolException e2) {
            HttpUtil.log.error("[" + url + "]请求失败" + e2.getMessage());
            throw e2;
        }
        catch (IOException e3) {
            HttpUtil.log.error("[" + url + "]请求失败" + e3.getMessage());
            throw e3;
        }
        finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
            }
            catch (IOException e4) {
                e4.printStackTrace();
            }
        }
    }

    static {
        log = LoggerFactory.getLogger((Class)HttpUtil.class);
    }
}
